//
//  CSCyclingPlaceRequest.swift
//  Cycling
//
//  Created by CSS on 4/7/23.
//

import Foundation
import CoreLocation

class CSCyclingPlaceRequest {
    
    class func requestLocationInfo(withCoordinator coordinator: CLLocationCoordinate2D, complete: ((String?) -> Void)?) {
        CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: coordinator.latitude, longitude: coordinator.longitude)) { placemarks, error in
            guard let placemark = placemarks?.first else {
                complete?(nil)
                return
            }
            var names: [String] = []
            if let city = placemark.locality {
                names.append(city)
            }
            if let street = placemark.thoroughfare {
                names.append(street)
            }
            if let substreet = placemark.subThoroughfare {
                names.append(substreet)
            }
            if let name = placemark.name {
                names.append(name)
            }
            complete?(names.joined())
        }
    }
    
}
